<template>
  <div class="app-container calendar-list-container">
    <basic-container>
      <avue-crud ref="crud" :option="tableOption" :data="list" :page="page" v-model="form" :table-loading="listLoading"
                 :before-open="handleOpenBefore" @on-load="getList" @search-change="searchChange"
                 @refresh-change="refreshChange"
                 @size-change="sizeChange" @current-change="currentChange" @row-update="update" @row-save="create">
        <template slot="menuLeft">
          <el-button v-if="roleManager_btn_add" class="filter-item" type="primary" icon="el-icon-edit"
                     @click="handleCreate">添加
          </el-button>
        </template>
        <template slot="dsScopeForm" slot-scope="scope">
          <div v-if="form.dsType == 1">
            <el-tree ref="scopeTree" :data="dsScopeData" :check-strictly="true" :props="defaultProps"
                     :default-checked-keys="checkedDsScope" class="filter-tree" node-key="id" highlight-current
                     show-checkbox/>
          </div>
        </template>

        <template slot="menu" slot-scope="scope">
          <el-button v-if="roleManager_btn_edit" type="text" size="small" icon="el-icon-edit"
                     @click="handleUpdate(scope.row,scope.index)">编辑
          </el-button>
          <el-button v-if="roleManager_btn_del" type="text" size="small" icon="el-icon-delete"
                     @click="handleDelete(scope.row,scope.index)">删除
          </el-button>
          <el-button v-if="roleManager_btn_perm" type="text" size="small" icon="el-icon-plus"
                     @click="handlePermission(scope.row,scope.index)">权限
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <el-dialog :visible.sync="dialogPermissionVisible" :close-on-click-modal="false" title="分配权限">

      <el-tabs type="border-card" @tab-click="tabchange" value="menu">
        <el-tab-pane label="菜单权限" name="menu">
          <div class="dialog-main-tree">
            <!--            :default-checked-keys="checkedKeys"-->
            <el-tree ref="menuTree" :default-checked-keys="checkedKeys" :data="treeData" :check-strictly="true" :props="defaultProps"
                     :filter-node-method="filterNode" class="filter-tree" node-key="id" @check="handleCheck" @check-change="checkNode"
                     highlight-current show-checkbox
                     default-expand-all/>
            <!--<el-tree ref="menuTree" :data="treeData" :default-checked-keys="checkedKeys" :check-strictly="false" :props="defaultProps"
                     :filter-node-method="filterNode" class="filter-tree" node-key="id" highlight-current show-checkbox
                     default-expand-all />-->
          </div>
        </el-tab-pane>
        <el-tab-pane label="IDE权限" name="ide">
          <el-row>
            <el-col :span="6" v-for="(value,key) in roleIdePermsMap">
              <el-button type="warning" icon="el-icon-folder" @click="handleClick(key,value)">
                {{key}}
              </el-button>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button v-show="selectAll" type="info" size="small" @click="handleSelectAll()">全选</el-button>
        <el-button type="primary" size="small" @click="updatePermession(roleId)">更 新
        </el-button>
        <el-button type="default" size="small" @click="cancal()">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogIdePermissionVisible" :close-on-click-modal="false" title="分配IDE权限">
      <el-form :inline='true'>
        <el-form-item v-for="per in idepermList" style="width: 30%;">
          <el-checkbox v-model="per.assigned" :label="per.permName"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" size="small" @click="handleSelectGroup(true)">全选</el-button>
        <el-button type="info" size="small" @click="handleSelectGroup(false)">反选</el-button>
        <el-button type="default" size="small" @click="cancalIdeDialog()">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    addObj,
    delObj,
    fetchList,
    fetchRoleTree,
    fetchRoleIdePer,
    permissionUpd,
    putObj
  } from '@/api/admin/role'
  import {
    tableOption
  } from '@/const/crud/admin/role'
  import {
    fetchTree
  } from '@/api/admin/dept'
  import {
    fetchMenuTree
  } from '@/api/admin/menu'
  import {
    mapGetters
  } from 'vuex'

  export default {
    name: 'TableRole',
    data() {
      return {
        searchForm: {},
        tableOption: tableOption,
        dsScopeData: [],
        treeData: [],
        checkedKeys: [],
        checkedDsScope: [],
        defaultProps: {
          label: 'name',
          value: 'id'
        },
        roleIdePermsMap: {},
        selectAll: false,
        idepermList: [],
        idepermGroupName: '',
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        menuIds: '',
        list: [],
        listLoading: true,
        form: {},
        roleId: undefined,
        roleCode: undefined,
        rolesOptions: undefined,
        dialogPermissionVisible: false,
        dialogIdePermissionVisible: false,
        roleManager_btn_add: false,
        roleManager_btn_edit: false,
        roleManager_btn_del: false,
        roleManager_btn_perm: false,
        isSelectChildren:false
      }
    },
    created() {
      this.roleManager_btn_add = this.permissions['sys_role_add']
      this.roleManager_btn_edit = this.permissions['sys_role_edit']
      this.roleManager_btn_del = this.permissions['sys_role_del']
      this.roleManager_btn_perm = this.permissions['sys_role_perm']
    },
    computed: {
      ...mapGetters(['elements', 'permissions'])
    },
    methods: {
      getList(page, params) {
        this.listLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params, this.searchForm)).then(response => {
          this.list = response.data.data.records
          this.page.total = response.data.data.total
          this.listLoading = false
        }).catch(() => {
          this.listLoading = false
        })
      },
      refreshChange() {
        this.getList(this.page)
      },
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      currentChange(current) {
        this.page.currentPage = current
      },
      handleCreate() {
        this.$refs.crud.rowAdd()
      },
      handleOpenBefore(show) {
        fetchTree().then(response => {
          this.dsScopeData = response.data.data
          if (this.form.dsScope) {
            this.checkedDsScope = (this.form.dsScope).split(',')
          } else {
            this.checkedDsScope = []
          }
        })
        show()
      },
      handleUpdate(row, index) {
        this.$refs.crud.rowEdit(row, index)
      },
      cancal() {
        this.dialogPermissionVisible = false;
      },
      handlePermission(row) {
        fetchRoleTree(row.roleId)
          .then(response => {
            this.checkedKeys = response.data.data
            return fetchMenuTree()
          })
          .then(response => {
            this.treeData = response.data.data
            // 解析出所有的太监节点
            // this.checkedKeys = this.resolveAllEunuchNodeId(this.treeData, this.checkedKeys, [])
            this.dialogPermissionVisible = true
            this.roleId = row.roleId
            this.roleCode = row.roleCode
          })
          .then(res => {
            //设置菜单权限选中节点
            this.isSelectChildren = false;
            this.$refs.menuTree.setCheckedKeys(this.checkedKeys)
            //加载IDE权限列表
            fetchRoleIdePer(row.roleId).then(res => {
              this.roleIdePermsMap = res.data.data;
            })
          })
      },

      /**
       * 解析出所有的太监节点id
       * @param json 待解析的json串
       * @param idArr 原始节点数组
       * @param temp 临时存放节点id的数组
       * @return 太监节点id数组
       */
      resolveAllEunuchNodeId(json, idArr, temp) {
        for (let i = 0; i < json.length; i++) {
          const item = json[i]
          // 存在子节点，递归遍历;不存在子节点，将json的id添加到临时数组中
          if (item.children && item.children.length !== 0) {
            this.resolveAllEunuchNodeId(item.children, idArr, temp)
          } else {
            temp.push(idArr.filter(id => id === item.id))
          }
        }
        return temp
      },
      filterNode(value, data) {
        if (!value) return true
        return data.label.indexOf(value) !== -1
      },
      getNodeData(data, done) {
        done()
      },
      handleDelete(row, index) {
        this.$confirm('是否确认删除名称为"' + row.roleName + '"' + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(row.roleId)
        }).then(() => {
          this.getList(this.page)
          this.$notify.success('删除成功')
        })
      },
      create(row, done, loading) {
        if (this.form.dsType === 1) {
          this.form.dsScope = this.$refs.scopeTree.getCheckedKeys().join(',')
        }
        addObj(this.form).then(() => {
          this.getList(this.page)
          done()
          this.$notify.success('创建成功')
        }).catch(() => {
          loading()
        })
      },
      update(row, index, done, loading) {
        if (this.form.dsType === 1) {
          this.form.dsScope = this.$refs.scopeTree.getCheckedKeys().join(',')
        }
        putObj(this.form).then(() => {
          this.getList(this.page)
          done()
          this.$notify.success('修改成功')
        }).catch(() => {
          loading()
        })
      },
      updatePermession(roleId) {
        this.menuIds = ''
        this.menuIds = this.$refs.menuTree.getCheckedKeys().join(',').concat(',').concat(this.$refs.menuTree.getHalfCheckedKeys()
          .join(','))
        let permissionIds = this.concatIdePermissionId().join(',');
        permissionUpd(roleId, this.menuIds, permissionIds).then(() => {
          this.dialogPermissionVisible = false
          this.$store.dispatch('GetMenu', {
            type: false
          })
          this.$notify.success('修改成功')
        })
      },
      concatIdePermissionId() {
        let ids = [];
        let vs = this.roleIdePermsMap;
        for (var item in vs) {
          let list = vs[item];
          list.forEach(function (ele) {
            if (ele.assigned == true)
              ids.push(ele.id);
          });
        }
        return ids;
      },
      handleClick(group, value) {
        console.log(value)
        this.dialogIdePermissionVisible = true;
        this.idepermList = value
        this.idepermGroupName = group
      },
      tabchange(tab) {
        if (tab.name == 'ide')
          this.selectAll = true
        else
          this.selectAll = false
      },
      handleSelectAll() {
        let vs = this.roleIdePermsMap;
        for (var item in vs) {
          let list = vs[item];
          list.forEach(function (ele) {
            ele.assigned = true;
          });
        }
      },
      handleSelectGroup(flag) {
        this.idepermList.forEach(function (ele) {
          // this.roleIdePermsMap[ths.idepermGroupName].forEach(function(ele) {
          if (flag)
            ele.assigned = true;
          else
            ele.assigned = !ele.assigned;
        });

      },
      cancalIdeDialog() {
        this.dialogIdePermissionVisible = false;
      },
      handleCheck(){
        this.isSelectChildren = true;
      },
      //xiaobei added
      checkNode(node, checked, c) {
        setTimeout(()=>{
          if(!this.isSelectChildren)return
          if (node.children && node.children.length) {
            this.$refs.menuTree.setChecked(node.id, checked,)
            node.children.forEach(item => {
              this.$refs.menuTree.setChecked(item.id, checked,)
            })
          }
        },200);
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-dialog__wrapper {
    .el-dialog {
      width: 61% !important;

      .dialog-main-tree {
        max-height: 400px;
        overflow-y: auto;
      }
    }

    .el-form-item__label {
      width: 20% !important;
      padding-right: 20px;
    }

    .el-form-item__content {
      margin-left: 20% !important;
    }
  }
</style>
